// Get the path of the currently running do-file
local do_file_path `c(pwd)'

// Change the working directory to the location of the do-file
cd "`do_file_path'"


import dbase using "proc_data\roads_intersection_pt.dbf", clear

rename *, lower
rename join_count n_rdintr
collapse (sum) n_rdintr, by(objectid gid_0)

label variable n_rdintr "Number of intersections of any road : Jedwab and Storeygard"

save "proc_data\ROADS.dta", replace

* road length
import dbase using "proc_data\roads_length.dbf", clear

rename *, lower
collapse (sum) road_km, by(objectid gid_0 r2008)

* objectid == 0 is outside the fishnet from road lines
drop if objectid==0

* long to wide
rename road_km rdkm08q
reshape wide rdkm08q, i(objectid) j(r2008)

* Highway = 80. paved = 60. improved = 40. dirt = 12. no road = 6
*#8 = highway (highways are paved roads but with at least 2 x 3 lanes) 
*#1 = paved road
*#3 = improved road
*#5/0 = earthen road
replace rdkm08q1 = 0 if rdkm08q1 ==.
replace rdkm08q3 = 0 if rdkm08q3 ==.
replace rdkm08q5 = 0 if rdkm08q5 ==.
replace rdkm08q8 = 0 if rdkm08q8 ==.
replace rdkm08q0 = 0 if rdkm08q0 ==.
replace rdkm08q5 = rdkm08q5 + rdkm08q0
drop rdkm08q0
label variable rdkm08q1 "Paved road length  (km) : Jedwab and Storeygard 1"
label variable rdkm08q3 "Improved road length  (km) : Jedwab and Storeygard 3"
label variable rdkm08q5 "Dirt road length  (km) : Jedwab and Storeygard 5/0"
label variable rdkm08q8 "Highway road length  (km) : Jedwab and Storeygard 8"

*g roadcheck = rdkm08q1 + rdkm08q3 + rdkm08q5 + rdkm08q8
*sum roadcheck is > 0

sort objectid
merge 1:m objectid using "proc_data\ROADS.dta"

drop _merge gid_0
replace n_rdintr=0 if n_rdintr==.

sort objectid
save "proc_data\ROADS.dta", replace

** graph bar (sum) road_km, over(gid_0)

